package com.hexiaohe.springboot_vue_app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hexiaohe.springboot_vue_app.entity.User;
import com.hexiaohe.springboot_vue_app.mapper.UserMapper;
import com.hexiaohe.springboot_vue_app.service.UserService;
import com.hexiaohe.springboot_vue_app.utils.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public Result<User> login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        queryWrapper.eq("password",user.getPassword());
        User user1 = (User) userMapper.selectOne(queryWrapper);
        if (user1!=null){
            return Result.success(user1);
        }
        else {
            return Result.error("-1","你的用户名或者密码错误");
        }
    }
    @Override
    public Page<User> findPage(Page<User> page, String nickName) {
        Page<User> userPage = userMapper.findPage(page,nickName);
        return userPage;
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int updateById(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int deleteUserById(int userId) {
        return userMapper.deleteById(userId);
    }

    @Override
    public int registerUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public boolean getUserByUsername(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1!=null){
            return false;
        }else {
            return true;
        }

    }

    @Override
    public int updatePerson(User user) {
        return userMapper.updatePerson(user);
    }

    @Override
    public boolean selectUsername(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("password",password);
        queryWrapper.eq("username",username);
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1==null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public int updatePassword(String username, String xpassword) {
        return userMapper.updatePassword(username,xpassword);
    }
}
